<html>
<head><meta charset="utf-8"><title>How are strings immutable if I can assign a new value? [s... · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html">How are strings immutable if I can assign a new value? [s...</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="204474286"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204474286" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boris Junk <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204474286">(Jul 20 2020 at 20:49)</a>:</h4>
<p>Hi! Rust Book in Chapter 4.1 says "String literals ... they’re immutable". Me doing this:</p>
<div class="codehilite"><pre><span></span><code>...
let mut str1 = &quot;other string&quot;;
println!(&quot;string inside scope: {}&quot;, str1);
str1 = &quot;other string changed, thus mutable&quot;;
println!(&quot;string inside scope after change: {}&quot;, str1);
...
</code></pre></div>


<p>runs just fine :). Is the Book not updated or do i understand something wrong? BTW: Until now i'm really enjoying rust :).</p>



<a name="204474575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204474575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204474575">(Jul 20 2020 at 20:52)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> can this be moved to <a class="stream" data-stream-id="122651" href="/#narrow/stream/122651-general">#general</a> ?</p>



<a name="204474593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204474593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204474593">(Jul 20 2020 at 20:52)</a>:</h4>
<p>I believe you didn’t actually mutate the string, you only mutated the variable, but the first string is "still there"</p>



<a name="204474595"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204474595" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204474595">(Jul 20 2020 at 20:52)</a>:</h4>
<p>This topic was moved here from <a class="stream-topic" data-stream-id="182449" href="/#narrow/stream/182449-t-compiler.2Fhelp/topic/Ownership">#t-compiler/help &gt; Ownership</a> by <span class="user-mention silent" data-user-id="116122">simulacrum</span></p>



<a name="204474691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204474691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204474691">(Jul 20 2020 at 20:53)</a>:</h4>
<p>Yup, you've changed what <code>str1</code> refers to, not the value of what it (used to) refer to.</p>



<a name="204474789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204474789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204474789">(Jul 20 2020 at 20:54)</a>:</h4>
<p>much like how writing your house address on a piece of paper in your hand, and then replacing the paper with a new paper. The house is still there.</p>



<a name="204474997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204474997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boris Junk <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204474997">(Jul 20 2020 at 20:56)</a>:</h4>
<p>Ah i get it! Of course! The Literal is everything inside the double question marks themselve, somewhere on the stack (?),  thus i just "redirected" the target where 'str1' to, to another Literal. Correct?</p>



<a name="204475043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204475043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boris Junk <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204475043">(Jul 20 2020 at 20:57)</a>:</h4>
<p>Oh dear, sorry for my bad english, it's late  :p</p>



<a name="204475045"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204475045" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204475045">(Jul 20 2020 at 20:57)</a>:</h4>
<p>The literal is baked in to the program binary itself, so neither the stack nor the heap, but otherwise yes.</p>



<a name="204475114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204475114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204475114">(Jul 20 2020 at 20:58)</a>:</h4>
<p><a href="https://stackoverflow.com/q/25468422/155423">Where is Rust storing all these bytes?</a></p>



<a name="204475366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204475366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boris Junk <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204475366">(Jul 20 2020 at 21:00)</a>:</h4>
<p>Oh your right, of course. Should have known this ... <br>
But thank you very much for this fast help! How do you handle 'closed' Questions in this chat? Do have to mark anything as 'solved'?</p>



<a name="204475421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204475421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204475421">(Jul 20 2020 at 21:00)</a>:</h4>
<p>We generally don't, but I might do so since you asked.</p>



<a name="204475515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204475515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boris Junk <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204475515">(Jul 20 2020 at 21:01)</a>:</h4>
<p>Ok, then just close. All open questions are solved :)</p>



<a name="204488756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204488756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204488756">(Jul 20 2020 at 21:57)</a>:</h4>
<p><span class="user-mention silent" data-user-id="323619">Boris Junk</span> <a href="#narrow/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E/near/204475366">said</a>:</p>
<blockquote>
<p>Oh your right, of course. Should have known this ... <br>
But thank you very much for this fast help! How do you handle 'closed' Questions in this chat? Do have to mark anything as 'solved'?</p>
</blockquote>
<p>this is a good idea, I'll start doing this in t-compiler/help :)</p>



<a name="204488860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204488860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204488860">(Jul 20 2020 at 21:58)</a>:</h4>
<p>You do run into the topic length limit pretty quickly</p>



<a name="204488861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20are%20strings%20immutable%20if%20I%20can%20assign%20a%20new%20value%3F%20%5Bs.../near/204488861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20are.20strings.20immutable.20if.20I.20can.20assign.20a.20new.20value.3F.20.5Bs.2E.2E.2E.html#204488861">(Jul 20 2020 at 21:58)</a>:</h4>
<p>hm doesn't that break links to the topic? Seems like a bad idea then</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>